package com.application.lucky.activity;

import android.Manifest;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Toast;

import com.application.lucky.R;
import com.application.lucky.manager.ContactsManager;
import com.application.mylibrary.base.BaseActivity;

import java.util.List;

import pub.devrel.easypermissions.EasyPermissions;

public class SplashActivity extends BaseActivity implements  EasyPermissions.PermissionCallbacks{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String[] perms = {Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE};
        if (EasyPermissions.hasPermissions(this, perms)) {
            // 已获取权限
            // ...
            startActivity(new Intent(this,AdActivity.class));
            finish();
        } else {
            // 没有权限，现在去获取
            // ...
            EasyPermissions.requestPermissions(this, "申请权限",
                    1, perms);
        }


    }

    @Override
    public void beforeInit() {

    }

    @Override
    public int getLayoutId() {
        return 0;
    }
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if(event.getKeyCode() == KeyEvent.KEYCODE_BACK ) {
            //do something.
            return true;
        }else {
            return super.dispatchKeyEvent(event);
        }
    }

    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
        // 一些权限被授予
        // ...
        startActivity(new Intent(this,AdActivity.class));
        finish();
    }

    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
        // 一些权限被禁止
        finish();
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        // 将返回结果转给EasyPermissions
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }
}
